package com.junxonline.instant.admin.controller.config;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junxonline.instant.dao.entity.model.config.Dict;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.DictGroupVO;
import com.junxonline.instant.dao.entity.vo.config.DictVO;
import com.junxonline.instant.admin.service.config.DictService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonRespPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 数据字典Controller
 *
 * @author JunX
 * @date 2021-07-18
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 根据数据字典分组查询数据字典列表（Select）
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresAuthentication
    @PostMapping(value = "/queryDictByGroupCode")
    public CommonResult queryDictByGroupCode(@RequestBody(required = false) DictGroupVO vo) {
        return CommonResult.success(dictService.queryDictByGroupCode(vo.getCode()));
    }

    /**
     * 查询数据字典列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dict:query")
    @OperationLog(label = "数据字典-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<DictVO> vo) {
        if (vo.getQueryParams().getParentId() == null) {
            IPage<Dict> returnPage = new Page<>();
            returnPage.setRecords(Collections.emptyList());
            returnPage.setTotal(Collections.emptyList().size());
            return CommonResult.success(CommonRespPage.success((returnPage)));
        }
        return CommonResult.success(dictService.queryList(vo));
    }

    /**
     * 查询数据字典
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dict:query")
    @OperationLog(label = "数据字典-查询数据字典", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) DictVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(dictService.query(vo.getId()));
    }

    /**
     * 新增数据字典
     *
     * @param dict 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dict:add")
    @OperationLog(label = "数据字典-新增数据字典", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Dict dict) {
        if (dict == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        dictService.add(dict);
        return CommonResult.success();
    }

    /**
     * 编辑数据字典
     *
     * @param dict 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dict:edit")
    @OperationLog(label = "数据字典-编辑数据字典", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Dict dict) {
        if (dict == null || dict.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        dictService.edit(dict);
        return CommonResult.success();
    }

    /**
     * 删除数据字典
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dict:delete")
    @OperationLog(label = "数据字典-删除数据字典", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        dictService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
